#!/usr/bin/env python
#

"""One-line documentation for from_to_tests module.

A detailed description of from_to_tests.
"""

__author__ = 'scottakirkwood@gmail.com (Scott Kirkwood)'

import mm2notes
import unittest
import codecs
import glob
import logging
import difflib

class FromToTests(unittest.TestCase):
    def run_one_html(self, mm_file, html_file):
        mm2n = mm2notes.Mm2Notes()
        mm2n.set_order_by_time(False)
        lines = mm2n.open(mm_file)
        test_name = 'last_output.html'
        outfile = codecs.open(test_name, 'w', 'utf-8')
        mm2n.write(outfile, lines)
        assert False == self.compare_files(html_file, test_name)

    def run_one_dict(self, mm_file, dict_file):
        mm2n = mm2notes.Mm2Notes()
        mm2n.set_order_by_time(False)
        mm2n.xmlparse(file(mm_file).read())
        info = mm2n.convert()
        info_expected = eval(file(dict_file).read())
        if info != info_expected:
            print info
            print info_expected
        self.assertEqual(info, info_expected)

    def compare_files(self, good_file, test_file):
        diff = difflib.Differ()
        good_lines = open(good_file).read().splitlines()
        test_lines = open(test_file).read().splitlines()
        diffs = diff.compare(good_lines, test_lines)
        differs = False
        for line in diffs:
            if not line.startswith('  '):
                if not differs:
                    differs = True
                    print 'Files %s and %s differ' % (good_file, test_file)
            if differs:
                print line
        return differs

    def test_all(self):
        for fname in glob.glob('tests/*.mm'):
            fnamehtml = fname.replace('.mm', '.html')
            fnamedict = fname.replace('.mm', '_dict.py')
            print 'Testing ', fname
            logging.warning('Testing %r' % fname)

            self.run_one_html(fname, fnamehtml)
            self.run_one_dict(fname, fnamedict)
    

if __name__ == '__main__':
    unittest.main() 
